package com.lxdp.service.impl;

import com.lxdp.dto.Result;
import com.lxdp.entity.ShopType;
import com.lxdp.mapper.ShopTypeMapper;
import com.lxdp.service.IShopTypeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.TimeUnit;

import static com.lxdp.utils.RedisConstants.CACHE_SHOP_CATEGORY_KEY;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hong
 * @since 2023-04-22
 */
@Service
@Slf4j
public class ShopTypeServiceImpl extends ServiceImpl<ShopTypeMapper, ShopType> implements IShopTypeService {

    @Resource
    private RedisTemplate redisTemplate;
    @Resource
    private StringRedisTemplate stringRedisTemplate;


    @Override
    public Result getTypeLisl() {
//        List<ShopType> shopTypes = null;
//        List<String> typeList = stringRedisTemplate.opsForList().range(CACHE_SHOP_CATEGORY_KEY, 0, -1);
//        log.info("map后的shop-type"+ typeList);
//        if(!typeList.isEmpty()){
//
//            return  Result.ok(shopTypes);
//        }
        List<ShopType> shopTypes = null;
        shopTypes =  (List<ShopType>) redisTemplate.opsForValue().get(CACHE_SHOP_CATEGORY_KEY);
        if(shopTypes != null){
            return Result.ok(shopTypes);
        }
        List<ShopType> list = this.list();
//        stringRedisTemplate.opsForList().leftPushAll(CACHE_SHOP_CATEGORY_KEY,list.toString());

        redisTemplate.opsForValue().set(CACHE_SHOP_CATEGORY_KEY,list,30, TimeUnit.MINUTES);
        return Result.ok(list);
    }
}
